package com.yunxiangyue.info_user

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import com.yunxiangyue.User_room.UserDao
import com.yunxiangyue.User_room.UserDatabase
import com.yunxiangyue.app.R

class mainActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var phone:String
    private lateinit var userDao: UserDao
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        getActionBarStyle()
        findViewById<TextView>(R.id.fillInfo).setOnClickListener(this)
        findViewById<TextView>(R.id.all).setOnClickListener(this)
        phone = intent.getStringExtra("phone").toString()

        userDao = UserDatabase.getUserDatabase(this).userDao()
    }

    override fun onResume() {
        super.onResume()
        val userList = userDao.queryUserByPhone_list(phone)
        for (user in userList){
            findViewById<TextView>(R.id.userName).setText(user.nike)
            findViewById<TextView>(R.id.introDuctory).setText("个人简介： "+user.intro)
            findViewById<TextView>(R.id.gender).setText("性别： "+user.gender)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.user_msg, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.info -> {
                val intent = Intent(this, UserInfo::class.java)
                intent.putExtra("phone",phone)
                startActivityForResult(intent, 1)
            }
            R.id.QR_code -> {
                val intent = Intent(this, user_QR_code::class.java)
                startActivity(intent)
            }
            R.id.background -> {
                val intent = Intent(this, cover::class.java)
                startActivityForResult(intent, 1)
            }
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }


    open fun getActionBarStyle() {
        val actionBar = supportActionBar
        if (actionBar != null) {
//            设置ActionBar是否显示返回键
            actionBar.setDisplayHomeAsUpEnabled(true)
//            导入返回键图片
            actionBar.setHomeAsUpIndicator(R.drawable.ic_fanhui_2)
//            设置是否显示标题
            actionBar.setDisplayShowTitleEnabled(false)
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.name -> {
                val intent = Intent(this, rectify_name::class.java)
                intent.putExtra("phone",phone)
                startActivityForResult(intent, 1)
            }
            R.id.fillInfo -> {
                val intent=Intent(this,UserInfo::class.java)
                intent.putExtra("phone",phone)
                startActivityForResult(intent,1)
            }
            R.id.all -> {
//                跳转到修改信息的页面
                val intent=Intent(this,UserInfo::class.java)
                intent.putExtra("phone",phone)
                startActivityForResult(intent,1)
            }
        }
    }

    override fun startActivity(intent: Intent?) {
        super.startActivity(intent)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==1){
            if(resultCode==1){
                val data=data?.getBundleExtra("bun")
                val name=data?.getString("name")
                val intro=data?.getString("intro")
                findViewById<TextView>(R.id.userName).text=name
                findViewById<TextView>(R.id.introDuctory).text="个人简介："+intro
            }
        }
    }
}


